package com.hope.hotel.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ObjectMetadata;
import com.hope.hotel.common.Result;
import com.hope.hotel.entity.DAO.LoginDAO;
import com.hope.hotel.entity.DAO.RegisterDAO;
import com.hope.hotel.entity.DAO.TravelerDAO;
import com.hope.hotel.entity.DAO.UserInfoDAO;
import com.hope.hotel.service.UserService;
import com.hope.hotel.utils.AliOSSUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.util.*;

/**
 * @作者 TD
 * @日期 2025/5/18
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    private final AliOSSUtils aliOSSUtils;

    @PostMapping ("/login")
    public Result Login(@RequestBody LoginDAO loginDAO){
        return userService.login(loginDAO);
    }

    @PostMapping("/register")
    public Result Register(@RequestBody RegisterDAO registerDAO){
        return userService.register(registerDAO);
    }

    @PostMapping("/send-code")
    public Result<String> SendCode(String email){
        return userService.sendCode(email);
    }

    @GetMapping("/test")
    public String test(HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        System.out.println("useId" + userId);
        return "test_success";
    }

    @GetMapping("/info")
    public Result getUserInfo(HttpServletRequest request){
        String header = request.getHeader("Authorization");
        if (header == null || !header.startsWith("Bearer ")) {
            return Result.error("请先登录");
        }
        String token = header.substring(7);
        return userService.checkLogin(token);
    }

    @PostMapping("/add-traveler")
    public Result addTraveler(@RequestBody TravelerDAO TravelerDAO, HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        return userService.addTraveler(TravelerDAO, userId);
    }

    @PostMapping("/update-info")
    public Result updateInfo(@RequestBody UserInfoDAO userInfoDAO, HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        return userService.updateInfo(userInfoDAO, userId);
    }

    @PostMapping("/upload-avatar")
    public Result uploadAvatar(MultipartFile image) {
        try {
            String url = aliOSSUtils.upload(image);
            return Result.success(url);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("上传失败");
        }
    }

    /**
     * 获得用户保存的其它用户信息
     * @param request
     * @return
     */

    @GetMapping("/get-travelers")
    public Result getTravelers(HttpServletRequest request){
//        HttpServletRequest request1 = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String userId = (String) request.getAttribute("userId");
        return userService.getTravelers(userId);
    }

    /**
     * 更新旅客信息
     * @param travelerDAO 旅客信息
     * @param request HTTP请求
     * @return 操作结果
     */
    @PostMapping("/update-traveler")
    public Result updateTraveler(@RequestBody TravelerDAO travelerDAO, HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        return userService.updateTraveler(travelerDAO, userId);
    }

    /**
     * 删除旅客信息
     * @param id 旅客ID
     * @param request HTTP请求
     * @return 操作结果
     */
    @DeleteMapping("/delete-traveler/{id}")
    public Result deleteTraveler(@PathVariable String id, HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        return userService.deleteTraveler(id, userId);
    }

}
